package cn.ex;

import java.util.Arrays;

public class Ex5 {
    public static void main(String[] args) {
        String[] ls = new String[]{"age", "Eat", "tea", "tan", "ate", "nat", "bat", "book", "bkoo"};
        System.out.println(Arrays.toString(ls));
        for (int i = 0; i < ls.length - 1; i++) {
            for (int j = i + 1; j <= ls.length - 1; j++) {
                String a = ls[i];
                String b = ls[j];
                String t = a;
                if (a.indexOf("-") != -1) {
                    t = a.substring(0, a.indexOf("-"));
                }
                if (isOk(t, b)) {
                    ls[i] = a + "-" + b;
                    ls[j] = "";
                }
            }
        }

        StringBuilder sbu = new StringBuilder();
        for(String t : ls){
            if(t.equals("") || t.equals("-")){
            }else{
                sbu.append(t+":");
            }
        }
        String strok = sbu.append("\b").toString();
        String[] ok = strok.split(":");
        System.out.print("[");
        for(String t : ok){
            System.out.print(Arrays.toString(t.split("-")));
        }

    }

    public static boolean isOk(String a, String b) {
        boolean f = false;
        char[] aa = a.toLowerCase().toCharArray();
        char[] bb = b.toLowerCase().toCharArray();
        if (aa.length == bb.length) {
            Arrays.sort(aa);
            Arrays.sort(bb);
            f = new String(aa).equalsIgnoreCase(new String(bb));
        }
        return f;
    }
}
